﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace KingOfCSharp.iHunter
{
    internal class PluginWrapper : IPlugin
    {
        IPlugin plugin;

        public PluginWrapper(IPlugin plugin)
        {
            try
            {
                this.plugin = plugin;
            }
            catch (Exception e)
            {
                this.OnPluginError(e);
            }

        }

        public IPlugin Plugin
        {
            get { return plugin; }
        }

        protected void OnPluginError(Exception e)
        {
            if (ErrorOccur != null)
                ErrorOccur.Invoke(this, new PluginErrorEventArgs(plugin, e.Message));
        }

        #region IPlugin Members

        public Guid Guid
        {
            get 
            {
                try
                {
                    return plugin.Guid;
                }
                catch (Exception e)
                {
                    this.OnPluginError(e);
                    return Guid.Empty;
                }
            }
        }

        public void Initialize(IPluginClient client)
        {
            try
            {
                plugin.Initialize(client);
            }
            catch (Exception e)
            {
                this.OnPluginError(e);
            }
        }

        public System.Windows.Forms.UserControl GetPluginConfigureEditor()
        {
            try
            {
                return plugin.GetPluginConfigureEditor();
            }
            catch (Exception e)
            {
                this.OnPluginError(e);
                return null;
            }
        }

 
        public event PluginErrorEventHandler ErrorOccur;

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            try
            {
                plugin.Dispose();
            }
            catch (Exception e)
            {
                this.OnPluginError(e);
            }
        }

        #endregion

        #region IPlugin 成员


        public string Name
        {
            get
            {
                try
                {
                    return plugin.Name;
                }
                catch (Exception e)
                {
                    this.OnPluginError(e);
                    return null;
                }
            }
        }

        public string Description
        {
            get
            {
                try
                {
                    return plugin.Description;
                }
                catch (Exception e)
                {
                    this.OnPluginError(e);
                    return null;
                }
            }
        }

        #endregion
    }
}
